package com.xxxx.server.exceptions;

import com.xzl.tool.util.RespBean;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;

/**
 * @Author Lionel
 * @Date 2020/10/20 10:45
 * Created by IntelliJ IDEA 2020.1.
 * Project: yeb
 * Description：全局异常处理
 */
//控制器的增强类
@RestControllerAdvice
public class GlobalException {

    @ExceptionHandler(SQLException.class)
    public RespBean sqlException(SQLException e) {
        return RespBean.error("数据库异常，操作失败");
    }

    /**
     * 500异常处理
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public RespBean exceptionHandler(Exception e) {
        if (e instanceof SQLException) {
            return RespBean.error("数据库异常，操作失败");
        }
        if (e instanceof SQLIntegrityConstraintViolationException) {
            return RespBean.error("该数据有关联数据，操作失败");
        }
        if (e instanceof MyException){
            return RespBean.error(((MyException) e).getCode(),e.getMessage());
        }
        e.printStackTrace();
        return RespBean.error("未知异常，请联系管理员！");
    }

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public RespBean handle2(MethodArgumentNotValidException ex) {
        BindingResult bindingResult = ex.getBindingResult();
        if (bindingResult != null) {
            if (bindingResult.hasErrors()) {
                FieldError fieldError = bindingResult.getFieldError();
                String field = fieldError.getField();
                String defaultMessage = fieldError.getDefaultMessage();
                return RespBean.error(field + ":" + defaultMessage);
            } else {
                return RespBean.error("RCode.ERROR.getCode(),RCode.ERROR.getMsg()");
            }
        } else {
            return RespBean.error("RCode.ERROR.getCode(),RCode.ERROR.getMsg()");
        }

    }

//    /**
//     * 404异常处理
//     */
//    @ExceptionHandler(value = NoHandlerFoundException.class)
//    @ResponseStatus(HttpStatus.NOT_FOUND)
//    public ModelAndView errorHandler(HttpServletRequest request, NoHandlerFoundException exception, HttpServletResponse response) {
//        return commonHandler(request, response,
//                exception.getClass().getSimpleName(),
//                HttpStatus.NOT_FOUND.value(),
//                exception.getMessage());
//    }

    /**
     * 405异常处理
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public RespBean errorHandler(HttpServletRequest request, HttpRequestMethodNotSupportedException exception, HttpServletResponse response) {
        String simpleName = exception.getClass().getSimpleName();
        int value = HttpStatus.METHOD_NOT_ALLOWED.value();
        String message = exception.getMessage();
        System.out.println(simpleName);
        System.out.println(value);
        System.out.println(message);
        return RespBean.error("405异常处理");
    }

    /**
     * 415异常处理
     */
    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    public RespBean errorHandler(HttpServletRequest request, HttpMediaTypeNotSupportedException exception, HttpServletResponse response) {
        String simpleName = exception.getClass().getSimpleName();
        int value = HttpStatus.UNSUPPORTED_MEDIA_TYPE.value();
        String message = exception.getMessage();
        System.out.println(simpleName);
        System.out.println(value);
        System.out.println(message);
        return RespBean.error("415异常处理");
    }
}
